﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Member
{
    public partial class SamPhamThanhCong : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            GiaoDienDTO giaoDienDto = GiaoDienBUS.SelectGiaoDienMa("Theme001");
            if (giaoDienDto != null)
            {
                this.Theme = giaoDienDto.TenTheme;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session.Count > 0)
            {
                string mataikhoan = this.Session["userid"].ToString().Trim();

                if (!IsPostBack)
                {
                    TaiKhoanBUS taiKhoanBus = new TaiKhoanBUS();
                    ThongTinTaiKhoanBUS thongTinTaiKhoanBus = new ThongTinTaiKhoanBUS();

                    DataTable dt = taiKhoanBus.LoaiTaiKhoan(mataikhoan);
                    string images = dt.Rows[0]["Avatar"].ToString().Trim();
                    if (images == "")
                    {
                        dt.Rows[0]["Avatar"] = "~/images/user.png";
                    }
                    FormView2.DataSource = dt;
                    FormView2.DataBind();

                    FormView3.DataSource = dt;
                    FormView3.DataBind();
                }

                string maSanPham = Request.QueryString["id"].Trim();
                DataTable dataTable = SanPhamBUS.getSanPhamOfMaSanPhams(maSanPham);
                DataList1.DataSource = dataTable;
                DataList1.DataBind();

                FormView1.DataSource = dataTable;
                FormView1.DataBind();

                FormViewHoaDon.DataSource = dataTable;
                FormViewHoaDon.DataBind();
            }
            else
            {
                Response.Redirect("/Guest/LoginPage.aspx");
            }
        }

        protected void btnXacNhan_Click(object sender, EventArgs e)
        {
            ccJoin.ValidateCaptcha(txtMaXacNhan.Text.Trim());

            if(ccJoin.UserValidated)
            {
                string maSanPham = Request.QueryString["id"].Trim();
                SanPhamDTO sanPhanDto = SanPhamBUS.getSanPhamOfMa(maSanPham);
                TaiKhoanDTO taiKhoanDto = TaiKhoanBUS.GetTaiKhoanOfMa(sanPhanDto.MaTaiKhoan);
                 
                int diemban = 0;
                Int32.TryParse(taiKhoanDto.DiemTinCayBan, out diemban);
                diemban += 100;
                taiKhoanDto.DiemTinCayBan = diemban.ToString();
                bool res = TaiKhoanBUS.UpdateTaiKhoan(taiKhoanDto);
                if (res)
                {
                    string mataikhoan = this.Session["userid"].ToString().Trim();
                    List<ChiTietGiaoDichDTO> chiTiet = ChiTietGiaoDichBUS.SelectChiTietSP(maSanPham,mataikhoan);
                    for (int i = 0; i < chiTiet.Count; i++)
                    {
                        if (chiTiet[i].TinhTrang == "Thành công")
                        {
                            bool result = ChiTietGiaoDichBUS.UpdateTrangThai(chiTiet[i]);
                            if(result)
                            {
                                lblThongBao.Text = "BẠN ĐÃ XÁC NHẬN THÀNH CÔNG SẢN PHẨM";
                                Page_Load(sender,e);
                            }
                        }
                    }
                   
                }
            }
        }

        protected void ImageButtonResfresh_Click(object sender, ImageClickEventArgs e)
        {
           
        }
    }
}
